package com.epam.iunna.bookshop.controller.actions;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.iunna.bookshop.controller.application.Constants;
import com.epam.iunna.bookshop.controller.filters.AuthFilter;
import com.epam.iunna.bookshop.controller.utils.dispatch.ActionInt;
import com.epam.iunna.bookshop.model.Order;
import com.epam.iunna.bookshop.model.OrderDao;
import com.epam.iunna.bookshop.model.User;

public class OrderHistory  implements ActionInt{

    @Override
    public String perform(HttpServletRequest request,
            HttpServletResponse response) {

        User user = (User)request.getSession().getAttribute( AuthFilter.USER_SESSION_KEY);
         OrderDao orderDao = new OrderDao();
         List<Order> orderList = new LinkedList<Order>();
         try {
             orderList = orderDao.getByUser(user.getId());
        } catch (SQLException e1) {
            System.out.println("DB error cannot get order list from db");
            return Constants.FATAL_ERROR_PAGE_PATH;
        }
        request.setAttribute("orderList",orderList);
        
        return "/orderList.jsp";
    }

}

